package com.github.abel533.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.SmartLifecycle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import static com.github.abel533.C.print;

/**
 * @author liuzh
 */
public class BeanLifecycle implements InitializingBean, DisposableBean, SmartLifecycle {

    public void initMethod() {
        print("@Bean(initMethod)");
    }

    public void destroyMethod() {
        print("@Bean(destroyMethod)");
    }

    @PostConstruct
    public void init() {
        print("@PostConstruct");
    }

    @PreDestroy
    public void destroy2() {
        print("@PreDestroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        print("InitializingBean#afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        print("DisposableBean#destroy");
    }

    boolean running = false;

    @Override
    public void start() {
        running = true;
        print("SmartLifecycle#start");
    }

    @Override
    public void stop() {
        running = false;
        print("SmartLifecycle#stop");
    }

    @Override
    public boolean isRunning() {
        print("SmartLifecycle#isRunning");
        return running;
    }
}
